// mypage/setPassword/setPassword.js
var app=getApp();
var util=require("../../utils/util");
var config=require("../../api/config");
var my=require("../../api/my");
var encryption=require("../../utils/md5");


Page({
  /*页面的初始数据*/
  data: {
    show_page1: !1,
    show_page2: !0,
    show_page3: !1,
    show_page4: !1,
    mobile:'',
    mob1: '',
    show_ver: false,
    focus: false,
    time: 60,
    length:6,
    isFocus:false,
    Value:"",
    show_btn: false,
    userinfo: '',
    login_info: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    that.setData({login_info:wx.getStorageSync('login_info')})
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({userinfo:res.data})
      }
    })
  },
  // 原密码验证
  nav_page_show:function(){
    this.setData({show_page1:true,show_page2:false})
  },
  // 手机验证
  nav_phone_show:function(){
    var that=this,userinfo=that.data.userinfo;
    var mob1 = util.insertStr(util.insertStr(userinfo.phone,3," "),8," ");
    that.setData({show_page2:false,show_page3:true,focus:true,mobile:userinfo.phone,mob1:mob1})
  },
  // 验证手机
  send_mobile:function(){
    var that = this,mobile = that.data.mobile;
    var neg=(/^1[3456789]\d{9}$/);
    if(!neg.test(mobile)||!mobile){
      util.page_show_toast('手机号码输入有误',1500)
      return false;
    }
    //
    that.send_code(mobile)
  },
  // 手机号输入
  // mobile_focus(e){
  //   var that = this;
  //   var val = e.detail.value;
  //   var mob1 = util.insertStr(util.insertStr(val,3," "),8," ");
  //   that.setData({mobile:val, mob1:mob1})
  // }, 
  mobile_tap(e){
    var that = this;
    that.setData({ mobile:e.detail.value })
  },
  // 验证码输入
  ver_focus(e){
    var that = this;
    var val = e.detail.value;
    that.setData({ Value:val, })
    if(val.length == 6){
      //
      that.very_code(that.data.mobile,val)
    }
  }, 
  ver_tap(){
    var that = this;
    that.setData({ isFocus:true, })
  },
  // 倒计时
  interval:function(){
    var that = this,time=that.data.time;
    var tms=setInterval(function(){
      time--;
      that.setData({time: time})
      if(time==0){
        clearInterval(tms)
        that.setData({time: 60,show_btn: true})
      }
    },1000)
  },
  // 重发
  again_send:function(){
    var that = this,show_btn=that.data.show_btn;
    if(show_btn){
      //
      that.interval();
      that.setData({show_btn: false})
    }
  },
  // 密码输入
  inpue_old_passWord:function(e){
    this.setData({old_password: e.detail.value})
    this.lite_btn()
  },
  inpue_new_passWord:function(e){
    this.setData({new_password: e.detail.value})
    this.lite_btn()
  },
  inpue_again_passWord:function(e){
    this.setData({again_password: e.detail.value})
    this.lite_btn()
  },
  // 点亮按钮
  lite_btn:function(){
    var that = this,mobile=that.data.mobile,old_password=that.data.old_password,new_password=that.data.new_password,again_password=that.data.again_password;
    if(!mobile){
      if(old_password&&new_password&&again_password){
        that.setData({show_btn: true})
      }else{
        that.setData({show_btn: false})
      }
    }else{
      if(new_password&&again_password){
        that.setData({show_btn: true})
      }else{
        that.setData({show_btn: false})
      }
    }
  },
  // 保存
  confirm_password:function(){
    var that=this,old_password=that.data.old_password,new_password=that.data.new_password,again_password=that.data.again_password,userinfo=that.data.userinfo,mobile=that.data.mobile;
    if(!mobile){
      if(!old_password){
        util.page_show_toast('原密码未输入',1500)
        return false;
      }
      if(new_password == old_password){
        util.page_show_toast('新密码和原密码不能一样',1500)
        return false;
      }
    }
    if(!new_password){
      util.page_show_toast('新密码未输入',1500)
      return false;
    }
    if(new_password.length<6){
      util.page_show_toast('新密码长度不足',1500)
      return false;
    }
    if(!again_password){
      util.page_show_toast('确认密码未输入',1500)
      return false;
    }
    if(again_password.length<6){
      util.page_show_toast('确认密码长度不足',1500)
      return false;
    }
    if(new_password != again_password){
      util.page_show_toast('新密码和确认密码不一致',1500)
      return false;
    }
    //
    if(!mobile){
      that.set_password_old({
        "id": userinfo.id,
        "phone": userinfo.phone,
        "oldPassword": encryption.md5(old_password),
        "password": encryption.md5(new_password)
      })
    }else{
      that.set_password_code({
        "phone": userinfo.phone,
        "password": encryption.md5(new_password)
      })
    }
  },
  // 发送验证码请求
  send_code:function(mobile){
    var that=this;
    config.send_code({"phone":mobile}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        app.globalData.change_psd=true
        that.setData({show_page4: true,isFocus: true});
        that.interval();
        setTimeout(function(){
          util.page_show_toast('验证码已发送',1500)
        },500)
      }
    })
  },
  // 手机验证码验证请求
  very_code:function(mobile,code){
    var that=this;
    config.very_code({phone:mobile,code:code}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        that.setData({show_page1:true,show_page5:true})
      }
    })
  },
  // 验证手机设置密码
  set_password_code:function(params){
    var that=this,login_info=that.data.login_info;
    my.set_password_code(params).then(res=>{
      if(res.data.code==200){
        util.page_show_toast('修改成功',1500)
        if(login_info){
          login_info.password=params.password;
          wx.setStorageSync('login_info', login_info)
        }
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }else{
        util.page_show_toast(res.data.message,2000)
      }
    })
  },
  // 原密码验证设置密码
  set_password_old:function(params){
    var that=this,login_info=that.data.login_info;
    my.set_password_old(params).then(res=>{
      if(res.data.code==200){
        util.page_show_toast('修改成功',1500)
        if(login_info){
          login_info.password=params.password;
          wx.setStorageSync('login_info', login_info)
        }
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }else{
        util.page_show_toast(res.data.message,2000)
      }
    })
  },
})